﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_CategoryManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCategory();
        }
    }
    private void loadNumRecord()
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        grvCategory.DataSource = cdt.Select_AllCategory();
        grvCategory.DataBind();
    }
    private void LoadCategory()
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        int? count = 0;
        grvCategory.DataSource = cdt.Select_Count_Category(ref count);
        grvCategory.DataBind();
        this.lbSoLuongCategory.Text = count.ToString();
    }
    private void AddCategory(String name)
    {
        try
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            cdt.Insert_Category(name);
            cdt.SubmitChanges();
        }
        catch (Exception ex)
        {

        }
        LoadCategory();
    }
    private void EditCategory(int id,String name)
    {
        try
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            cdt.Update_Category(id, name);
            cdt.SubmitChanges();
        }
        catch (Exception ex)
        {
        }
        LoadCategory();
    }
    private void DeleteCategory(int id)
    {
        try
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            cdt.Delete_Category1(id);
            cdt.SubmitChanges();
        }
        catch (Exception ex)
        {
        }
        LoadCategory();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        String catName = txtAddCategory.Text;
        AddCategory(catName);
        txtAddCategory.Text = "";
        txtID.Text = "";
    }
    
    protected void grvCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grvCategory.Rows[e.RowIndex].Cells[2].Text);
        DeleteCategory(id);
        grvCategory.EditIndex = -1;
    }
    protected void grvCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grvCategory.SelectedRow;
    }
    protected void grvCategory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
        GridViewRow row = grvCategory.Rows[e.NewSelectedIndex];
        int id = int.Parse(row.Cells[2].Text);
        String name = row.Cells[3].Text;
        txtID.Text = id.ToString();
        txtAddCategory.Text = name;
    }
    protected void btnEdit_Click1(object sender, EventArgs e)
    {
        String id = txtID.Text;
        String name = txtAddCategory.Text;

        if (id != "" && name != "")
        {
            int catID = int.Parse(id);
            EditCategory(catID, name);

            txtAddCategory.Text = "";
            txtID.Text = "";
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtAddCategory.Text = "";
        txtID.Text = "";
    }
    protected void grvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        grvCategory.DataSource = cdt.Select_AllCategory();
        grvCategory.PageIndex = e.NewPageIndex; 
        grvCategory.DataBind();
    }
}